// 添加调试信息
console.log("脚本开始运行");
var window = floaty.window(
    <frame gravity="center">
      <horizontal>
      <button w="auto" id="closeBut" text="隐藏" />
      <button w="auto" id="autoQH" text="识别切抢环1" />
      <button w="auto" id="autoQH2" text="识别切抢环" />
    </horizontal>
    </frame>
  );

setInterval(() => {}, 1000);
window.setSize(1000, 200);

window.closeBut.click(() => {
  console.log("关闭sss");
  window.setAdjustEnabled(!window.isAdjustEnabled());
});

function clickCard() {
  var isTrue = true;
  var isRunningFlag = 0;
  while (isTrue) {
    for (var i = 0; i < 10; i++) {
      press(675, 1603, 10);
      sleep(10);
    }
    isRunningFlag = gameIsRunning();
    if (isRunningFlag != 2) {
      console.log('异常');
      console.log(isRunningFlag, "flag");
      isTrue = isRunningFlag == 3;
    }
  }

  if (isRunningFlag === 0) {
    console.log('flag==0 开始执行中')
    secondStp();
  } else {
    console.log('等于1了，去check一下')
    checkGameOver();
  }

}

window.autoQH2.click(() => {
  threads.start(function () {
    firstStp();
  });
});

function gameIsRunning() {
  let screen = null;
  let imgZM = null;
  let imgMessage = null;
  try {
    // 1. 先判断是不是还在招募页面
    screen = captureScreen();
    if (!screen) {
      console.log("截图失败，无法判断是否在招募页面");
      return;
    }
    imgZM = images.read("./image/zhaomi.jpg");
    var zMButPoint = findImage(screen, imgZM, { threshold: 0.9 });
    if (zMButPoint) {
      console.log("找到了招募按钮，返回了")
      return 2;
    }
    // 释放第一次截图和图片资源
    screen.recycle();
    imgZM.recycle();

    sleep(1000);

    imgMessage = images.read("./image/message.jpg");
    screen = captureScreen();
    if (!screen) {
      console.log("截图失败，无法查找消息坐标");
      return;
    }
    var messagePoint = findImage(screen, imgMessage, { threshold: 0.8 });
    if (messagePoint) {
      console.log("找到了消息按钮，开始点击了")
      toast("找到了消息按钮，开始点击了")
      press(messagePoint.x + 10, messagePoint.y + 10, 10);
      console.log('点击消息的坐标：',messagePoint.x,messagePoint.y)
      // 释放第二次截图和图片资源
      screen.recycle();
      imgMessage.recycle();

      sleep(1000);

      screen = captureScreen();
      if (!screen) {
        console.log("截图失败，无法再次判断是否在招募页面");
        return;
      }
      imgZM = images.read("./image/zhaomi.jpg");
      zMButPoint = findImage(screen, imgZM, { threshold: 0.7 });
      if (zMButPoint) {

        // 释放第三次截图和图片资源
        screen.recycle();
        imgZM.recycle();
        return 0;
      } else {
        toast("游戏异常终止，");
        console.log("游戏异常终止，");
        // 释放第三次截图和图片资源
        screen.recycle();
        imgZM.recycle();
        return 1;
      }
    }
    // 释放第二次截图和图片资源
    screen.recycle();
    imgMessage.recycle();
    // 啥也没到找 返回3的时候，表示要重新尝试一次，先暂时这么写吧。
    return 3;
  } catch (error) {
    console.log("gameIsRunning 函数发生错误: ", error);
  } finally {
    if (screen) {
      screen.recycle();
    }
    if (imgZM) {
      imgZM.recycle();
    }
    if (imgMessage) {
      imgMessage.recycle();
    }
  }
}

// 当用户按下开始抢环的按钮以后
// 返回1 表示已经打开了消息界面
// 返回0  表示不在战斗界面
function firstStp() {
  let screen = null;
  try {
    // 获取message的坐标
    screen = captureScreen();
    if (!screen) {
      console.log("截图失败，无法查找消息坐标");
      return 0;
    }
    var imgMessage = images.read("./image/message.jpg");
    var messagePoint = findImage(screen, imgMessage, { threshold: 0.5 });
    if (messagePoint) {
      press(messagePoint.x + 10, messagePoint.y + 10, 10);  
      console.log('第一次点击消息：',messagePoint.x,messagePoint.y)
      screen.recycle();
      imgMessage.recycle();
      sleep(1000);
      secondStp();
      // return 1;
    } else {
      toast("未找到消息坐标，请手动点击《战斗》界面后重新执行脚本");
      console.log("未找到消息坐标，请手动点击《战斗》界面后重新执行脚本");
      screen.recycle();
      imgMessage.recycle();
      return 0;
    }
  } catch (error) {
    console.log("firstStp 函数发生错误: ", error);
  } finally {
    if (screen) {
      screen.recycle(); // 确保释放截图资源
    }
  }
}

// 第二步点击招募按钮
// 返回1 表示已经打开了招募界面
// 返回0  表示不在战斗界面
function secondStp() {
  var screen = null;
  var imgZM = null;
  try {
    screen = captureScreen();
    if (!screen) {
      console.log("截图失败，无法查找招募按钮坐标");
      return 0;
    }
    imgZM = images.read("./image/zhaomi.jpg");
    var zMButPoint = findImage(screen, imgZM, { threshold: 0.9 });
    if (zMButPoint) {
      press(zMButPoint.x + 50, zMButPoint.y + 50, 10);
      console.log(zMButPoint.x + 50, zMButPoint.y + 50); 
      sleep(2000);
      screen.recycle();
      imgZM.recycle();
      clickCard();
    } else {
      toast("未找到招募按钮，请手动点击《战斗》界面后重新执行脚本");
      console.log("未找到招募按钮，请手动点击《战斗》界面后重新执行脚本");
      screen.recycle();
      imgZM.recycle();
      return 0;
    }
  } catch (error) {
    console.log("secondStp 函数发生错误: ", error);
  } finally {
    if (screen) {
      screen.recycle(); // 确保释放截图资源

    }
  }
}

/*
  检查游戏是否结束了 返回1时表示游戏已经结束了 
*/
function checkGameOver() {
  var screen = null;
  var imgBack = null;
  try {
    var overFlag = true;
    while (overFlag) {
      screen = captureScreen();
      if (!screen) {
        console.log("截图失败，无法查找返回按钮坐标");
        sleep(5000);
        continue;
      }
      imgBack = images.read("./image/backBut.jpg");
      var backPoint = findImage(screen, imgBack, { threshold: 0.5 });
      if (backPoint) {
        //表示游戏已经结束 点击一次返回按钮
        press(backPoint.x + 10, backPoint.y + 10, 10);
        overFlag = false; // 结束循环
        screen.recycle(); 
        imgBack.recycle();
        console.log("已经点击返回了")
        sleep(5000)
        firstStp();
        // return 1; // 表示游戏结束
      } else {
        console.log("未找到返回按钮")
        //先销毁截图
        screen.recycle();
        imgBack.recycle();
        sleep(5000);
      }
    }
  } catch (error) {
    toast("发生了错误：" + error);
    console.log("发生了错误：" + error);
  } finally {
    if (screen) {
      screen.recycle(); // 确保最后释放截图资源
    }
    toast("游戏结束，开始下一轮");
    console.log("游戏结束，开始下一轮");
  }
}

setTimeout(() => {
  if (!requestScreenCapture()) {
    console.log("未获取到截图权限");
  } else {
    console.log("有权限截图");
  }
}, 1000);

// 在脚本结束时释放初始读取的图片资源
// 可以监听脚本退出事件来释放资源



